<?php

namespace App\Console\Commands;

use App\Jobs\DigArticleJobs;
use Carbon\Carbon;
use QL\QueryList;
use Illuminate\Support\Facades\Cache;
use Illuminate\Console\Command;

class DigCeshi extends Command
{
	//php artisan dig:ceshi 151 http://www.woshipm.com
    protected $client;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'dig:ceshi {col} {link}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'dig:ceshi 154 https://www.chinaz.com/2020/1122/1210127.shtml';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $link   = $this->argument('link');
        $col    = $this->argument('col');
        $data   = $this->check($link);
        if($data){
            $index  = $this->ithome($data);
            if($index){
                $collection = collect($index);
                $multiplied = $collection->map(function ($item) use ($col) {
                    DigArticleJobs::dispatch($item,$col);
                });
                $all    = $multiplied->all();
                $this->info('dig article ok!');
            }else{
                $this->error('dig article result!');
            }
        }else{
            $this->error('dig article error!');
        }
    }

    public function ithome($data){
    	$ql = QueryList::html($data)->rules([
            //采集规则
            'title'         => ['.article-header .title','text'],
            'keywords'      => ['meta[name=keywords]','content'],
            'description'   => ['meta[name=description]','content'],
            'content'       => ['#article-content','html'],
        ])->query()->getData(function ($item) {
            //利用回调函数下载文章中的图片并替换图片路径为本地路径
            //使用本例请确保当前目录下有image文件夹，并有写入权限
            $item['keywords']		= str_replace(' ', ',', $item['keywords']);
            $item['copy_from']		= '本站原创|#';
            $content = QueryList::html($item['content']);
            $content->find('img')->map(function($img){
                $src = $img->attr('src');
                $localSrc = 'article/images/'.md5($src).'.jpg';
				//文件是否存在
                $img->attr('class', 'load');
                $img->attr('lay-src', env('IMG_URL').$localSrc);
                $img->attr('src', env('IMG_URL').'files/loading.gif');
            });
            $content->find('blockquote')->remove();
            $content->find('div')->remove();
            $item['content']    = $content->find('')->html();
            return $item;
        });
        $data   = $ql->all();
        print_r($data);die();
        if(array_key_exists(0,$data)){
            return $data[0];
        }else{
            return false;
        }
    }
    //抓取网址
    function check($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);//抓取网址
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Baiduspider/3.0; +http://www.baidu.com/search/spider.html)");//伪造百度蜘蛛头部
        $ip = '220.181.7.121';
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:' . $ip, 'CLIENT-IP:' . $ip));//伪造百度蜘蛛IP
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//
//        curl_setopt($ch, CURLOPT_HEADER, true); //抓取服务器信息
        curl_setopt($ch, CURLOPT_NOBODY, false); //抓取网页信息
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //支持301跳转
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30); //网页等待时间
        $data = curl_exec($ch);
        $code = curl_getinfo($ch);
        curl_close($ch);
        if($code['http_code'] == 200){
            return $data;
//            $parse_url  = parse_url($code['url']);
//            return      $parse_url['scheme'].'://'.$parse_url['host'];
        }else{
            return false;
        }
    }
}
